package com.system.part.entity.dto;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.system.part.entity.Part;


public class PartDTO {
	private String partName;
	public String getPartName() {
		return partName;
	}

	public void setPartName(String partName) {
		this.partName = partName;
	}

	public static Specification<Part> getSpecification(PartDTO partDTO){
		Specification<Part> spec = new Specification<Part>() {
			
			public Predicate toPredicate(Root<Part> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			
				List<Predicate> list = new ArrayList<Predicate>();
				if(null !=partDTO &&!StringUtils.isEmpty(partDTO.getPartName())) {
					Predicate p1 = cb.like(root.get("partName").as(String.class), "%"+partDTO.getPartName()+"%");
					list.add(p1);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
